{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.093f01593ce34f7784d00c2fa695ce8d.315360000.1918884744.282335-22869366',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdAvblzL97VwGBXPZ08o0TgbtdUQN8ru1NqbNdGWyO51Y99ymYY74V0mSilFT681MNifW2DSISw0IazzuEcmOWHNN3xw==',\n",
       " 'access_token': '24.9437c66870e55a5aa623d42b02591985.2592000.1606116744.282335-22869366',\n",
       " 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component',\n",
       " 'session_secret': 'cc757182bc7a30b3430f35cddf0f5b74'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'\n",
    "client_id = \"b4F8yybjEcflwwBXoyBKGrUG\"\n",
    "client_secret = \"d18eg3NGfyr3zW9Ro8p9mF3dvO8iTtVy\"\n",
    "response = requests.get(host.format(client_id, client_secret))\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 5520194551016,\n",
       " 'timestamp': 1603525442,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': '9f0efc46a8dfe5fee821978d8d56ea35',\n",
       "    'location': {'left': 355.48,\n",
       "     'top': 35.3,\n",
       "     'width': 46,\n",
       "     'height': 46,\n",
       "     'rotation': -7},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -1.72, 'pitch': 7.77, 'roll': -5.77},\n",
       "    'face_shape': {'type': 'oval', 'probability': 0.71},\n",
       "    'face_type': {'type': 'human', 'probability': 1}}]}}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.人脸检测与属性分析\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1290955601,2197513585&fm=26&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "# image_type 图片类型：BASE64；URL；FACE_TOKEN。\n",
    "\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 2017575357984,\n",
       " 'timestamp': 1603525443,\n",
       " 'cached': 0,\n",
       " 'result': {'score': 59.19797897,\n",
       "  'face_list': [{'face_token': '9f0efc46a8dfe5fee821978d8d56ea35'},\n",
       "   {'face_token': '93d2501e1cb1685c26107d0b19cc854a'}]}}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.人脸对比\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1290955601,2197513585&fm=26&gp=0.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603015508446&di=eee4e2c852d804bc3b80a719df3df9ef&imgtype=0&src=http%3A%2F%2Fimg2-cloud.itouchtv.cn%2Ftvtouchtv%2Fimage%2F20170914%2F1505363583630378.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "# face_type 人脸的类型 LIVE；IDCARD；WATERMARK；CERT；INFRARED。\n",
    "\n",
    "access_token = '24.ba60ba4aa46e0c8129bc08a24305cf92.2592000.1606115723.282335-22869366' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 3579101001752,\n",
       " 'timestamp': 1603525446,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.创建用户组\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group006\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 5599057565757,\n",
       " 'timestamp': 1603526097,\n",
       " 'cached': 0,\n",
       " 'result': {'face_list': [{'face_token': '91cf0a5aa45b0371989e56760b30548c',\n",
       "    'ctime': '2020-10-23 17:46:47'}]}}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.获取用户人脸列表\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist\"\n",
    "\n",
    "params = \"{\\\"user_id\\\":\\\"user1\\\",\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 9410179059975,\n",
       " 'timestamp': 1603524044,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6.删除用户组\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/delete\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group1\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
